package com.transaction.controller;



import com.transaction.domain.Good;
import com.transaction.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/good")
public class GoodController {

    @Autowired
    private GoodService goodService;

    //按cid查询商品列表
    @RequestMapping("/findGoodByType")
    public @ResponseBody
    List<Good> findGoodByType(Good good){
        List<Good> goods=goodService.findGoodByType(good);
        return goods;
    }


    //商品列表
    @RequestMapping("/goodList")
    public @ResponseBody
    List<Good> goodList(){
        List<Good> goods=goodService.findAllGood();
        return goods;
    }

    //添加商品
    @Transactional
    @RequestMapping(value = "/addGood",method = RequestMethod.POST)
    public @ResponseBody
    Boolean addGood(Good good, MultipartFile img, HttpServletRequest request){
        String fileName = img.getOriginalFilename();
        good.setGsrc(fileName);
        good.setSid(1);
        String filePath = "D://transaction_imgs//"; // 上传后的路径
        String path= filePath+fileName;
        System.out.println(good);
        System.out.println(path);
        Boolean result= goodService.addOneGood(good,img,path);

        return result;
    }

}
